﻿Public Class ViewDashboardZoneOptions

    Private _viewDashboardZone As ViewDashboardZone

    Sub New(viewDashboardZone As ViewDashboardZone)
        ' TODO: Complete member initialization 
        InitializeComponent()

        _viewDashboardZone = viewDashboardZone

        Me.DataContext = _viewDashboardZone

        'Me.PropertyGrid.DataContext = _viewDashboardZone.DataContext

        Me.ZonePropertyGrid.SetDashboardZone(_viewDashboardZone.DataContext)

        Try
            Me.ZoneDataGrid.ItemsSource = _viewDashboardZone.DataContext.DataProperty.Value
        Catch ex As Exception

        End Try
    End Sub

    Sub New()

        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().

    End Sub

    Private Sub EditPropertyButtonClick(sender As Object, e As Windows.RoutedEventArgs)
        Dim bt As Windows.Controls.Button = sender
        bt.ContextMenu.DataContext = bt.DataContext
        bt.ContextMenu.IsOpen = True

    End Sub

    Private Sub InitialiserContextMenuItemClick(sender As Object, e As Windows.RoutedEventArgs)
        Dim mi As Windows.Controls.MenuItem = sender
        Dim prop = ComponentModel.TypeDescriptor.GetProperties(GetType(ViewDashboardZone))(CStr(mi.DataContext))
        Dim depprop = TryCast(prop, ComponentModel.DependencyPropertyDescriptor)

        If depprop IsNot Nothing Then
            prop.SetValue(_viewDashboardZone, depprop.Metadata.DefaultValue)
        Else
            prop.ResetValue(_viewDashboardZone)


            '       prop.ResetValue.SetValue(_viewDashboardZone, CDbl(0))
        End If
        '    ComponentModel.TypeDescriptor.GetProperties(GetType(ViewDashboardZone))(CStr(mi.DataContext)).ResetValue(_viewDashboardZone)
    End Sub

    Private Function StyleMap(prop As String) As ComponentModel.PropertyDescriptor
       
        If prop = "Background" Or prop = "BorderThickness" Or prop = "CornerRadius" Or prop = "BorderBrush" Then
            Return ComponentModel.TypeDescriptor.GetProperties(_viewDashboardZone)("Zone" & prop)
        End If

        Return Nothing
    End Function

    Private Sub CreerRessourceContextMenuItemClick(sender As Object, e As Windows.RoutedEventArgs)
        Dim mi As Windows.Controls.MenuItem = sender
        Dim prop = ComponentModel.TypeDescriptor.GetProperties(GetType(ViewDashboardZone))(CStr(mi.DataContext))
        Dim depprop = TryCast(prop, ComponentModel.DependencyPropertyDescriptor)

        ModuleDashboarding._activeproject.Ressources.Add(InputBox("Nom de la ressource"), prop.GetValue(_viewDashboardZone))

        ModuleDashboarding._activeproject.SaveRessources()
        'Dim writer = New IO.StreamWriter("projectressources.xaml")
        'Windows.Markup.XamlWriter.Save(ModuleDashboarding._activeproject.Ressources, writer)
        'writer.Close()
        'writer.Dispose()

        'writer = New IO.StreamWriter("_" & _viewDashboardZone.Name & " .xaml")
        'Windows.Markup.XamlWriter.Save(_viewDashboardZone, writer)
        'writer.Close()

        'Dim xtoto = XElement.Load("_" & _viewDashboardZone.Name & " .xaml")
    End Sub

    Private Sub ModifierRessourceContextMenuItemClick(sender As Object, e As Windows.RoutedEventArgs)
        'Dim mi As Windows.Controls.MenuItem = sender
        'Dim prop = ComponentModel.TypeDescriptor.GetProperties(GetType(ViewDashboardZone))(CStr(mi.DataContext))
        'Dim depprop = TryCast(prop, ComponentModel.DependencyPropertyDescriptor)

        'If depprop IsNot Nothing Then
        '    prop.SetValue(_viewDashboardZone, depprop.Metadata.DefaultValue)
        'Else
        '    prop.ResetValue(_viewDashboardZone)


        '    '       prop.ResetValue.SetValue(_viewDashboardZone, CDbl(0))
        'End If
        ''    ComponentModel.TypeDescriptor.GetProperties(GetType(ViewDashboardZone))(CStr(mi.DataContext)).ResetValue(_viewDashboardZone)
    End Sub

    Private Sub AppliquerRessourceContextMenuItemClick(sender As Object, e As Windows.RoutedEventArgs)
        Dim mi As Windows.Controls.MenuItem = sender
        Dim prop = ComponentModel.TypeDescriptor.GetProperties(GetType(ViewDashboardZone))(CStr(mi.DataContext))
        Dim depprop = ComponentModel.DependencyPropertyDescriptor.FromProperty(prop)

        If depprop Is Nothing Then Return

        Dim cbx = New Windows.Controls.ComboBox()
        cbx.ItemsSource = (From kvp As System.Collections.DictionaryEntry In _
                           ModuleDashboarding._activeproject.Ressources _
                           Order By kvp.Key _
                           Select kvp.Key).ToList()
        cbx.SelectedIndex = 1
        ModuleDashboarding._uiservice.ShowMessage(cbx)
        _viewDashboardZone.SetResourceReference(depprop.DependencyProperty, CStr(cbx.SelectedItem))


        'ModuleDashboarding._activeproject.Ressources.Add(InputBox("Nom de la ressource"), prop.GetValue(_viewDashboardZone))

        'Dim writer = New IO.StreamWriter("projectressources.xaml")
        'Windows.Markup.XamlWriter.Save(ModuleDashboarding._activeproject.Ressources, writer)
        'writer.Close()
    End Sub


    Private Sub CreerStyleButtonClick(sender As Object, e As Windows.RoutedEventArgs)
        'Dim s = New Windows.Style()
        's.TargetType = GetType(ViewDashboardZone)
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.WidthProperty, _viewDashboardZone.Width))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.HeightProperty, _viewDashboardZone.Height))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.MarginProperty, _viewDashboardZone.Margin))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.HorizontalAlignmentProperty, _viewDashboardZone.HorizontalAlignment))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.VerticalAlignmentProperty, _viewDashboardZone.VerticalAlignment))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.ZoneBackgroundProperty, _viewDashboardZone.ZoneBackground))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.ZoneBorderBrushProperty, _viewDashboardZone.ZoneBorderBrush))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.ZoneBorderThicknessProperty, _viewDashboardZone.ZoneBorderThickness))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.ZoneCornerRadiusProperty, _viewDashboardZone.ZoneCornerRadius))
        's.Setters.Add(New Windows.Setter(ViewDashboardZone.TitleXamlStyleProperty, _viewDashboardZone.TitleXamlStyle))

        Dim sobject = New DashboardZoneStyleObject() With {.Style = New StyleObject(), .TitleStyle = _viewDashboardZone.TitleStyle}

        sobject.Style.Background = _viewDashboardZone.ZoneBackground
        sobject.Style.BorderBrush = _viewDashboardZone.ZoneBorderBrush
        sobject.Style.BorderThickness = _viewDashboardZone.ZoneBorderThickness
        sobject.Style.CornerRadius = _viewDashboardZone.ZoneCornerRadius
        sobject.Style.InlcudedProperties = New String() {"Background", "BorderBrush", "BorderThickness", "CornerRadius"}

        ModuleDashboarding._activeproject.Ressources.Add(InputBox("Nom du style"), sobject)

        ModuleDashboarding._activeproject.SaveRessources()
    End Sub

    Private Sub AppliquerStyleButtonClick(sender As Object, e As Windows.RoutedEventArgs)
        Dim q = From kvp As System.Collections.DictionaryEntry In ModuleDashboarding._activeproject.Ressources
                Where kvp.Value.GetType() Is GetType(DashboardZoneStyleObject) Select kvp

        'Dim q2 = (From s In q
        '          Where CType(s.Value, Windows.Style).TargetType Is GetType(ViewDashboardZone)
        '          Select CStr(s.Key)).ToList()


        If q.Count > 0 Then
            Dim lst = New Windows.Controls.ListBox()
            lst.DisplayMemberPath = "Key"
            lst.ItemsSource = q.ToList()
            lst.SelectedIndex = 0

            ModuleDashboarding._uiservice.ShowMessage(lst)

            Dim dzso = CType(ModuleDashboarding._activeproject.Ressources(CStr(lst.SelectedItem.Key)), DashboardZoneStyleObject)
            dzso.Style.Apply(_viewDashboardZone, AddressOf StyleMap)
            _viewDashboardZone.TitleStyle = dzso.TitleStyle

            '_viewDashboardZone.Style = CType(ModuleDashboarding._activeproject.Ressources(CStr(lst.SelectedItem)), Windows.Style)
        End If
    End Sub

    Private Sub Button_Click_3(sender As Object, e As Windows.RoutedEventArgs)
        Dim propgrid = New ViewDashboardPropertyGrid()

        propgrid.SetStyleObject(_viewDashboardZone.TitleStyle)

        ModuleDashboarding._uiservice.ShowMessage(propgrid, Width:=600, Height:=600)

        _viewDashboardZone.TitleStyle = _viewDashboardZone.TitleStyle
    End Sub
End Class

Public Class DashboardZoneStyleObject
    Inherits UIObject

    Private _Style As StyleObject
    Public Property Style As StyleObject
        Get
            Return _Style
        End Get
        Set(ByVal value As StyleObject)
            _Style = value
            OnPropertyChanged("Style")
        End Set
    End Property

    Private _TitleStyle As StyleObject
    Public Property TitleStyle As StyleObject
        Get
            Return _TitleStyle
        End Get
        Set(ByVal value As StyleObject)
            _TitleStyle = value
            OnPropertyChanged("TitleStyle")
        End Set
    End Property



End Class